package cn.lopr.im.android.mvp.view.activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.databinding.ActivityLoginBinding;
import cn.lopr.im.android.mvp.contact.ILoginContact;
import cn.lopr.im.android.mvp.presenter.LoginPresenterImpl;
import cn.lopr.im.android.utils.GsonUtil;
import cn.lopr.im.android.utils.PrefUtils;

public class LoginActivity extends Activity implements ILoginContact.ILoginView {

    // gradle3.6+  bind 对象
    private ActivityLoginBinding binding;

    private LoginPresenterImpl loginPresenterImpl;

    // 权限列表
    private static final String[] PERMISSION = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE, Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA, Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS};

    @Override
    protected View getContentLayoutId() {
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        binding.btnLogin.setOnClickListener(view -> loginPresenterImpl.login());
        binding.btnRegister.setOnClickListener(e -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

        String phone = PrefUtils.getString(this, "phone", "");
        String password = PrefUtils.getString(this, "password", "");
        if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)) {
            binding.edtPhone.setText(phone);
            binding.edtPassword.setText(password);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        loginPresenterImpl = new LoginPresenterImpl(this, this);
        loginPresenterImpl.isLogin();
    }

    @Override
    public String getPhone() {
        return binding.edtPhone.getText().toString();
    }

    @Override
    public String getPassword() {
        return binding.edtPassword.getText().toString();
    }

    @Override
    public void loginSuccess(User user) {
        PrefUtils.setString(this, "phone", getPhone());
        PrefUtils.setString(this, "password", getPassword());
        PrefUtils.setString(this, "user", GsonUtil.objectToJson(user));

        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra("user", user);
        this.startActivity(intent);
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void saveJwtKey(String token, String value) {
        PrefUtils.setString(this, token, value);
    }

    @Override
    public void isLogin(User result) {
        PrefUtils.setString(this, "phone", getPhone());
        PrefUtils.setString(this, "password", getPassword());
        PrefUtils.setString(this, "user", GsonUtil.objectToJson(result));

        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra("user", result);
        this.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isLacksOfPermission(PERMISSION[0])) {
            ActivityCompat.requestPermissions(this, PERMISSION, 0x12);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 0x12) {

        } else {
            finish();
        }
    }

    /**
     * 动态获取权限
     *
     * @return {@link boolean}
     * @author shuangyang7
     * @date 2020/12/29 21:50
     */
    private boolean isLacksOfPermission(String permission) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return ContextCompat.checkSelfPermission(this, permission) ==
                    PackageManager.PERMISSION_DENIED;
        }
        return false;
    }
}